package com.hk.yajin.function.members.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.manager.UserInfoManager;
import com.hk.yajin.model.Goods;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;
import com.hk.yajin.wxapi.WXPayEntryActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RechargeGlodActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_glod)
    TextView tvGlod;
    @BindView(R.id.ry_order)
    RecyclerView ryOrder;
    @BindView(R.id.tv_weixin_pay)
    TextView tvWeixinPay;
    @BindView(R.id.tv_price)
    TextView tvPrice;

    private GoodsQuickAdapter goodsAdapter;
    private List<Goods> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        showShortToast("重置功能暂未开放");
        finish();


//        initView();
//        initData();
    }

    private void initView() {
        initToolBar(R.string.buy_gold);
        if (UserInfoManager.getInstance().isLogin()) {
            tvId.setText(getString(R.string.memberid) + UserInfoManager.getInstance().getUserInfo().getUser_name());
            if (UserInfoManager.getInstance().getUserInfo().getUserGlod() != null) {
                tvGlod.setText("余额：" + UserInfoManager.getInstance().getUserInfo().getUserGlod().getUsable() + "金币");
            } else {
                tvGlod.setText("余额：" + "未知" + "金币");
            }

        } else {
            finish();
        }

        goodsAdapter = new GoodsQuickAdapter(datas, this);
        ryOrder.setAdapter(goodsAdapter);
        final GridLayoutManager manager = new GridLayoutManager(mContext, 4, GridLayoutManager.VERTICAL, false);
        ryOrder.setLayoutManager(manager);
        goodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                goodsAdapter.selectPosition = position;
                goodsAdapter.notifyDataSetChanged();
                tvPrice.setText(datas.get(position).getShop_price() + "元");
            }
        });
//        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                if (position == goodsAdapter.getItemCount() - 1) {
//                    return manager.getSpanCount();
//                } else {
//                    return 1;
//                }
//            }
//        });

    }

    private void initData() {
        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = UserInfoManager.getUserKeyMapIfLogin();
            HttpRequestUtils.post(true, UrlConstants.FUNCTION_ALL_GOODS, reqParam, false, new HttpCallBack() {
                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    LogUtil.d("成功：" + data);
                    if (!TextUtils.isEmpty(data)) {
                        List<Goods> temData = FastjsonUtils.getBeanList(data, Goods.class);
                        if (temData != null && temData.size() > 0) {
                            datas.addAll(temData);
                            goodsAdapter.notifyDataSetChanged();
                        }
                    } else {
                        goodsAdapter.loadMoreFail();
                        goodsAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    LogUtil.d("请求异常：" + tip);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    LogUtil.d("请求异常：" + bean.getMsg());
                }
            });
        }
    }


    @OnClick({R.id.tv_weixin_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_weixin_pay:
                hideKeyboard(tvWeixinPay);
                httpGetPayParm();
                break;
        }
    }

    private void httpGetPayParm() {

        // testPayMent();
        showShortToast("微信支付授权申请中");
    }

    private void testPayMent() {
        final IWXAPI msgApi = WXAPIFactory.createWXAPI(getApplicationContext(), WXPayEntryActivity.APP_ID);
        msgApi.registerApp(WXPayEntryActivity.APP_ID);
        PayReq req = new PayReq();
        req.appId = WXPayEntryActivity.APP_ID;
        req.partnerId = "1900000109";
        req.prepayId = "1101000000140415649af9fc314aa427";
        req.nonceStr = "1101000000140429eb40476f8896f4c9";
        req.timeStamp = "1398746574";
        req.packageValue = "Sign=WXPay";
        req.sign = "7FFECB600D7157C5AA49810D2D8F28BC2811827B";
        req.extData = "app data"; // optional

        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        msgApi.sendReq(req);
    }

}
